﻿//************************************************************************************************
// Copyright © 2014 Steven M. Cohn. All Rights Reserved.
// 
//************************************************************************************************

namespace River.Folderize.UI
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Input;


	/// <summary>
	/// Interaction logic for LibraryPanel.xaml
	/// </summary>
	
	public partial class LibraryPanel : UserControl
	{

		private LibraryController controller;
		private List<LibraryFolder> libraries;


		/// <summary>
		/// 
		/// </summary>

		public LibraryPanel ()
		{
			InitializeComponent();

			controller = new LibraryController();
			libraries = controller.GetLibraries();

			libraryList.Items.SortDescriptions.Add(
				new SortDescription("Name", ListSortDirection.Ascending));

			foreach (LibraryFolder library in libraries)
			{
				libraryList.Items.Add(library);
			}

			// TODO: should use binding instead
			libraryList.Items.Refresh();
		}


		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Implicit actions
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		private void SelectionChanged (object sender, SelectionChangedEventArgs e)
		{
			if (libraryList.SelectedIndex == -1)
			{
				removeButton.IsEnabled = false;
				iconButton.IsEnabled = false;
				pinButton.IsEnabled = false;
			}
			else
			{
				var library = libraryList.SelectedItem as LibraryFolder;
				removeButton.IsEnabled = true;
				iconButton.IsEnabled = true;
				pinButton.IsEnabled = true;
				pinButton.IsChecked = library.IsPinned;
				pathLabel.Content = library.Path;
			}
		}


		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Commands
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		private void AddCustomLibrary (object sender, RoutedEventArgs e)
		{
			var folder = new LibraryFolder(Guid.NewGuid().ToString("B"));
			editor.Create(folder);

			// once the user click OK, PropertiesCompleted() is invoked...
		}


		private void RemoveCustomLibrary (object sender, RoutedEventArgs e)
		{
			var library = libraryList.SelectedItem as LibraryFolder;
			if (library != null)
			{
				var result = MessageBox.Show(Application.Current.MainWindow,
					"Remove library " + library.Name + "?",
					"Confirm", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);

				if (result == MessageBoxResult.Yes)
				{
					controller.RemoveLibrary(library.DescriptionPath);
					libraryList.Items.Remove(libraryList.SelectedItem);

					libraries.RemoveAll(p => p.Guid.Equals(library.Guid));
				}
			}
		}


		private void EditItem (object sender, MouseButtonEventArgs e)
		{
			if (e.ClickCount >= 2)
			{
				EditProperties(sender, e);
			}
		}

	
		private void EditProperties (object sender, RoutedEventArgs e)
		{
			var library = libraryList.SelectedItem as LibraryFolder;
			if ((library.Image == null) && !String.IsNullOrEmpty(library.ImageRes))
			{
				library.Image = ImageHelper.LoadIcon(library.ImageRes, ImageHelper.ImageType.Library);
			}

			editor.Modify(library);
		}
		
		
		private void PropertiesCompleted (object sender, PropertiesEditedRoutedEventArgs e)
		{
			var library = e.Folder as LibraryFolder;

			if (e.IsNewFolder)
			{
				controller.AddLibrary(library);
				libraries.Add(library);
				libraryList.Items.Add(library);
			}
			else
			{
				controller.SaveProperties(library, e.Original as LibraryFolder);
			}

			libraryList.Items.Refresh();
		}


		private void TogglePinned (object sender, RoutedEventArgs e)
		{
			var library = libraryList.SelectedItem as LibraryFolder;
			if (library != null)
			{
				library.IsPinned = !library.IsPinned;
				controller.SetPin(library.DescriptionPath, library.IsPinned);
			}
		}
	}
}
